package cn.iocoder.yudao.module.ao.controller.admin.classmajor;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import org.springframework.web.multipart.MultipartFile;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ao.controller.admin.classmajor.vo.*;
import cn.iocoder.yudao.module.ao.dal.dataobject.classmajor.ClassMajorDO;
import cn.iocoder.yudao.module.ao.service.classmajor.ClassMajorService;

@Tag(name = "管理后台 - 班级专业关联")
@RestController
@RequestMapping("/ao/class-major")
@Validated
public class ClassMajorController {

    @Resource
    private ClassMajorService classMajorService;

    @PostMapping("/create")
    @Operation(summary = "创建班级专业关联")
    @PreAuthorize("@ss.hasPermission('ao:class-major:create')")
    public CommonResult<Long> createClassMajor(@Valid @RequestBody ClassMajorSaveReqVO createReqVO) {
        return success(classMajorService.createClassMajor(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新班级专业关联")
    @PreAuthorize("@ss.hasPermission('ao:class-major:update')")
    public CommonResult<Boolean> updateClassMajor(@Valid @RequestBody ClassMajorSaveReqVO updateReqVO) {
        classMajorService.updateClassMajor(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除班级专业关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ao:class-major:delete')")
    public CommonResult<Boolean> deleteClassMajor(@RequestParam("id") Long id) {
        classMajorService.deleteClassMajor(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除班级专业关联")
                @PreAuthorize("@ss.hasPermission('ao:class-major:delete')")
    public CommonResult<Boolean> deleteClassMajorList(@RequestParam("ids") List<Long> ids) {
        classMajorService.deleteClassMajorListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得班级专业关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ao:class-major:query')")
    public CommonResult<ClassMajorRespVO> getClassMajor(@RequestParam("id") Long id) {
        ClassMajorDO classMajor = classMajorService.getClassMajor(id);
        return success(BeanUtils.toBean(classMajor, ClassMajorRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得班级专业关联分页")
    @PreAuthorize("@ss.hasPermission('ao:class-major:query')")
    public CommonResult<PageResult<ClassMajorRespVO>> getClassMajorPage(@Valid ClassMajorPageReqVO pageReqVO) {
        PageResult<ClassMajorDO> pageResult = classMajorService.getClassMajorPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ClassMajorRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出班级专业关联 Excel")
    @PreAuthorize("@ss.hasPermission('ao:class-major:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportClassMajorExcel(@Valid ClassMajorPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ClassMajorDO> list = classMajorService.getClassMajorPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "班级专业关联.xls", "数据", ClassMajorRespVO.class,
                        BeanUtils.toBean(list, ClassMajorRespVO.class));
    }

    @GetMapping("/get-import-template")
    @Operation(summary = "获得导入中高技班级课模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<ClassMajorImportExcelVO> list = Arrays.asList(
                ClassMajorImportExcelVO.builder().classId(1L).majorId(1L).majorDirectionId(1L).build(),
                ClassMajorImportExcelVO.builder().classId(2L).majorId(2L).majorDirectionId(2L).build()
        );
        // 输出
        ExcelUtils.write(response, "中高技班级课导入模板.xls", "中高技班级课列表", ClassMajorImportExcelVO.class, list);
    }

    @PostMapping("/import")
    @Operation(summary = "导入中高技班级课")
    @PreAuthorize("@ss.hasPermission('ao:class-major:import')")
    @Parameter(name = "file", description = "Excel 文件", required = true)
    @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    @Parameter(name = "sheetName", description = "工作表名称", required = false)
    public CommonResult<ClassMajorImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                      @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport,
                                                      @RequestParam(value = "sheetName", required = false) String sheetName) throws Exception {
        List<ClassMajorImportExcelVO> list = ExcelUtils.read(file, ClassMajorImportExcelVO.class);
        return success(classMajorService.importClassMajorList(list, updateSupport));
    }

    @PostMapping("/get-excel-sheets")
    @Operation(summary = "获取Excel文件的工作表名称")
    @Parameter(name = "file", description = "Excel 文件", required = true)
    public CommonResult<List<String>> getExcelSheets(@RequestParam("file") MultipartFile file) throws Exception {
        // 简单实现，返回默认工作表名称
        List<String> defaultSheets = new ArrayList<>();
        defaultSheets.add("Sheet1");
        return success(defaultSheets);
    }

}